package com.cx.utils;


import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

/**
 * @ClassName: GetToken
 * @Description:
 * @Author: cx
 * @Date: 2023/10/13 15:20
 **/
public class GetToken {

    @Value("${service.ip}")
    private static String serviceIp;


    //获取token
    private static final String GET_TOKEN = "%s/authentication/v1/token";
    //获取人像库信息
    private static final String REPOSITORIES_ID = "/face/v1/repositories/%s";
    //获取image根据url
    private static final String GET_IMAGE_URL = "/face/v1/images/%s";

    public static void main(String[] args) throws IOException {
        System.out.println(serviceIp);

        String url = String.format(GET_TOKEN, "http://172.16.10.166");
        System.out.println(url);
        OkHttpClient client = new OkHttpClient();


        // 设置请求体的类型为application/json
        MediaType ss = MediaType.parse("application/json; charset=utf-8");

       // 构建请求体
        String jsonBody = "{\"audience\":\"web\", \"password\":\"bc79cf32e1f9d19fa3812adcbf7b94bd\", \"subject\":\"api\", \"username\":\"admin\"}";
        RequestBody requestBody = RequestBody.create(ss, jsonBody);

        System.out.println("body的值" + requestBody.toString());

        Request request = new Request.Builder()
                .url(url)
                .header("Content-Type", "application/json")
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        Response execute = call.execute();

        String responseBody = execute.body().string();
        System.out.println(JSONObject.parse(responseBody).getString("token"));

    }


}
